
import {prizeRedeem} from './winningInfoListServ.js'
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { message } from 'antd';
import { download, makeAddress } from 'utils/util'
import axios from 'axios'
import config from 'config/config'

const defultState = {
    gameActivityId:'',
    biz:{},
    showModal:false,
    defaultRedeemRemark:{},
}

// 声明module
const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    //   dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                    dispatch({ type: 'updateStore', payload:{gameActivityId:query.gameActivityId} });
                }
            })
        },
    },


    // 方法列表
    effects: {
        *prizeRedeem({ payload }, { put, call, select }) {
            try{
                const { data, success } = yield call(prizeRedeem, { ...payload });
                if(success){
                    let defultState = yield select(state => state[tmpModule.namespace])
                    message.success('修改备注成功')
                    yield put({ type: 'updateStore', payload: { showModal:false} })
                    defultState.biz.refreshListDatas(true)
                }else{
                    message.warning('修改备注失败')
                }
            }catch(e){

            }
        },
        *winningExportXls({ payload }, { put, call, select }) {
            try{
                axios({
                    method: 'post',
                    url: config.baseUrl+'mideadc-game-center-service/activity/gameActivityUserPrize/exportXls',
                    responseType: 'blob',
                    data: {...payload},
                    headers: {}
                }).then(response => {
                    let fileName = '奖品信息'
                    let data = response.data
                    // let {data} = yield call(playerExportXls, {...payload})
                    if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        window.navigator.msSaveBlob(
                            new Blob([data]),
                            fileName + '.xls'
                        );
                    } else {
                        console.log('new Blob([data]',new Blob([data]))
                        let url = window.URL.createObjectURL(new Blob([data]))
                        let link = document.createElement('a');
                        link.style.display = 'none';
                        link.href = url;
                        link.setAttribute('download', fileName + '.xls');
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link); //下载完成移除元素
                        window.URL.revokeObjectURL(url); //释放掉blob对象
                    }
                    message.success('操作成功')
                    console.log('config',config)
                    console.log(window.location.origin)
                })
            }catch(e){

            }
        }
    },

    // reducers: {
    //   updateStore(preState, action) {
    //     return Object.assign({}, preState, action.payload)
    //   },
    // }
    reducers: {
        updateStore(preState, action) {
            return { ...preState, ...action.payload };
        }
    }
}

export default tmpModule
